﻿using XChipTool.Enties;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
public class DbContext<T> where T : class, new()
{
    public DbContext()
    {
        Db = new SqlSugarClient(new ConnectionConfig()
        {
            ConnectionString = $"Data Source={AppDomain.CurrentDomain.BaseDirectory}\\DB\\xchiptool.db;Version =3;",
            DbType = DbType.Sqlite,
            InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
            IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
            
        });
       
        //调式代码 用来打印SQL 
        //Db.Aop.OnLogExecuting = (sql, pars) =>
        //{
        //    Console.WriteLine(sql + "\r\n" +
        //        Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
        //    Console.WriteLine();
        //};

    }
    //注意：不能写成静态的
    public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
	public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } }//用来操作当前表的数据

   public SimpleClient<addressregister> addressregisterDb { get { return new SimpleClient<addressregister>(Db); } }//用来处理addressregister表的常用操作
   public SimpleClient<bincurrency> bincurrencyDb { get { return new SimpleClient<bincurrency>(Db); } }//用来处理bincurrency表的常用操作
   public SimpleClient<bindefault> bindefaultDb { get { return new SimpleClient<bindefault>(Db); } }//用来处理bindefault表的常用操作
   public SimpleClient<bindriving> bindrivingDb { get { return new SimpleClient<bindriving>(Db); } }//用来处理bindriving表的常用操作
   public SimpleClient<control_parameter> control_parameterDb { get { return new SimpleClient<control_parameter>(Db); } }//用来处理control_parameter表的常用操作
   public SimpleClient<gammahistory> gammahistoryDb { get { return new SimpleClient<gammahistory>(Db); } }//用来处理gammahistory表的常用操作
   public SimpleClient<parameter_export> parameter_exportDb { get { return new SimpleClient<parameter_export>(Db); } }//用来处理parameter_export表的常用操作
   public SimpleClient<parameter_export_patch> parameter_export_patchDb { get { return new SimpleClient<parameter_export_patch>(Db); } }//用来处理parameter_export_patch表的常用操作
   public SimpleClient<serialdata> serialdataDb { get { return new SimpleClient<serialdata>(Db); } }//用来处理serialdata表的常用操作
   public SimpleClient<xc151> xc128Db { get { return new SimpleClient<xc151>(Db); } }//用来处理xc151表的常用操作
    public SimpleClient<xc151_ex> xc151_exDb { get { return new SimpleClient<xc151_ex>(Db); } }//用来处理xc151表的常用操作
    public SimpleClient<xc128register> xc128registerDb { get { return new SimpleClient<xc128register>(Db); } }//用来处理xc128register表的常用操作

    public SimpleClient<xc151register> xc151registerDb { get { return new SimpleClient<xc151register>(Db); } }//用来处理xc151register表的常用操作
    public SimpleClient<xcmainregister> xcmainregisterDb { get { return new SimpleClient<xcmainregister>(Db); } }//用来处理xcmainregister表的常用操作

    //public SimpleClient<xc128_1> xc128_1Db { get { return new SimpleClient<xc128_1>(Db); } }//用来处理xc128表的常用操作
    //public SimpleClient<xc128_1_32> xc128_1_32Db { get { return new SimpleClient<xc128_1_32>(Db); } }//用来处理xc128_1_64表的常用操作
    public SimpleClient<parameter_export_scene> parameter_export_sceneDb { get { return new SimpleClient<parameter_export_scene>(Db); } }//用来处理parameter_export_scene表的常用操作
    public SimpleClient<carBackLineTemplate> car_back_line_templateDb { get { return new SimpleClient<carBackLineTemplate>(Db); } }//用来处理car_back_line_template表的常用操作                                                                                                                                     /// <summary>
    public SimpleClient<parameter_contrast> parameter_contrastDb { get { return new SimpleClient<parameter_contrast>(Db); } }//用来处理parameter_export表的常用操作                                                                                                                           

    public SimpleClient<portconfig> portconfigDb { get { return new SimpleClient<portconfig>(Db); } }//用来处理portconfig表的常用操作
    public SimpleClient<xc151binparsing> xc151binparsingDb { get { return new SimpleClient<xc151binparsing>(Db); } }//用来处理xc151binparsing表的常用操作

    public SimpleClient<polling_register> polling_registerDb { get { return new SimpleClient<polling_register>(Db); } }//用来处理xc151binparsing表的常用操作
    public virtual List<T> GetList()
    {
        return CurrentDb.GetList();
    }

  

    /// <summary>
    /// 根据表达式查询
    /// </summary>
    /// <returns></returns>
    public virtual List<T> GetList(Expression<Func<T, bool>> whereExpression)
    {
        return CurrentDb.GetList(whereExpression);
    }

   

    /// <summary>
    /// 根据表达式查询分页
    /// </summary>
    /// <returns></returns>
    public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel pageModel)
    {
        return CurrentDb.GetPageList(whereExpression, pageModel);
    }

    /// <summary>
    /// 根据表达式查询分页并排序
    /// </summary>
    /// <param name="whereExpression">it</param>
    /// <param name="pageModel"></param>
    /// <param name="orderByExpression">it=>it.id或者it=>new{it.id,it.name}</param>
    /// <param name="orderByType">OrderByType.Desc</param>
    /// <returns></returns>
    public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel pageModel, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
    {
        return CurrentDb.GetPageList(whereExpression, pageModel,orderByExpression,orderByType);
    }


    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <returns></returns>
    public virtual List<T> GetById(dynamic id)
    {
        return CurrentDb.GetById(id);
    }

    /// <summary>
    ///  /// <summary>
    /// 根据条件获取model类型
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public virtual T GetSingle(Expression<Func<T, bool>> whereExpression)
    {
        return CurrentDb.GetSingle(whereExpression);
    }
    /// 根据主键删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public virtual bool Delete(dynamic id)
    {
        return CurrentDb.Delete(id);
    }
    public virtual bool Deletes(Expression<Func<T, bool>> whereExpression)
    {
        return CurrentDb.Delete(whereExpression);
    }

    /// <summary>
    /// 根据实体删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public virtual bool Delete(T data)
    {
        return CurrentDb.Delete(data);
    }

    /// <summary>
    /// 根据主键删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public virtual bool Delete(dynamic[] ids)
    {
        return CurrentDb.AsDeleteable().In(ids).ExecuteCommand()>0;
    }

    /// <summary>
    /// 根据表达式删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public virtual bool Delete(Expression<Func<T, bool>> whereExpression)
    {
        return CurrentDb.Delete(whereExpression);
    }


    /// <summary>
    /// 根据实体更新，实体需要有主键
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public virtual bool Update(T obj)
    {
        return CurrentDb.Update(obj);
    }

    /// <summary>
    ///批量更新
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public virtual bool Update(List<T> objs)
    {
        return CurrentDb.UpdateRange(objs);
    }

    /// <summary>
    /// 插入
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public virtual bool Insert(T obj)
    {
        
        return CurrentDb.Insert(obj);
    }


    /// <summary>
    /// 批量
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public virtual bool Insert(List<T> objs)
    {
        return CurrentDb.InsertRange(objs);
    }


    //自已扩展更多方法 
}


